 clear all;clf;clc;

%ASKHSH 1
a=[2 1 1]';

b=[1 -1 -1]

b*a

% to dianusma b den einai monadiko. px [4 2 -10]

%ASKHSH 2
%prosthetoume allo ena dianusma wste to sunolo na parameinei orthogwnio

 a = [2 1 1]';
 
 b=b';
 c = cross(a,b);
 figure(1); 
 clf;
 drawVector([a b c],{'a','b','c'});
 view(-160, 7)

 %ASKHSH 3
 
 
 A = [a b c];
 X = sampleUnitSphere(3,200); 
 AX = A*X;
 figure(1);
 clf;
 hold on;
 plot3( X(1,:), X(2,:), X(3,:),'b.');
 view(-60,10)
 plot3(AX(1,:),AX(2,:),AX(3,:),'g.');
 drawVector(A,'r.');
 hold off; 
 axis square equal; 
 grid on;
 
 %ASKHSH 4
 B = [a b c];
 C = diag([1/norm(a) 1/norm(b) 1/norm(c)]);
 A = B*C;
 
 
 
 AX=A*X;;
 figure(1);
 clf
 hold on;
 plot3( X(1,:), X(2,:), X(3,:),'b.');
 view(-60,10)
 plot3(AX(1,:),AX(2,:),AX(3,:),'g.');
 hold off; 
 axis square equal; 
 grid on;
 
 
 AX=A*B*X;
 figure(1);
 clf
 hold on;
 plot3( X(1,:), X(2,:), X(3,:),'b.');
 view(-60,10)
 plot3(AX(1,:),AX(2,:),AX(3,:),'g.');
 hold off; 
 axis square equal; 
 grid on;
 
 AX=A*A*B*X;
 figure(1);
 clf
 hold on;
 plot3( X(1,:), X(2,:), X(3,:),'b.');
 view(-60,10)
 plot3(AX(1,:),AX(2,:),AX(3,:),'g.');
 hold off; 
 axis square equal; 
 grid on;
 
 AX=A*A*A*B*X;
 figure(1);
 clf
 hold on;
 plot3( X(1,:), X(2,:), X(3,:),'b.');
 view(-60,10)
 plot3(AX(1,:),AX(2,:),AX(3,:),'g.');
 hold off; 
 axis square equal; 
 grid on;
 
 %ASKHSH 5
 A=[1 -1;
     1 1];
 
 
 
 
 %NORMES PINAKWN
 
 %ASKHSH 1
 
 B=[1 3;
     1 1];
 C=reshape(1:4,2,2);
 
 
 %ANTISTROFOS PINAKAS
 
 A = [ 1 1; -2 2 ];
 
 e1 = [1;0];
 e2 = [0;1]; 
 figure(1);
 clf;
 hold on;
 drawVector(A, {'a','b'});
 drawVector([e1 e2], 'gs', {'e_1', 'e_2'});
 %drawVector(inv(A),{'c','d'});
 hold off;
 
 %ASKHSH 2
 
 invA=inv(A)
 P1 = A*diag(invA(:,1)) 
 figure(1); hold on; 
 % Plot the projections 
 drawVector(P1, 'rd');
 drawLine([e1 P1(:,1)]);  
 drawLine([e1 P1(:,2)]);
 hold off
 
 %ASKHSH 1
 
  % A = [ 2 4; 3 6 ]; 
 %  b = [4 7]';
 A = [ 4 1;2 4 ]; b = [2 -2]';

  x = A\b; 
  figure(1);
  clf; hold on; 
  drawVector(A, {'a_1', 'a_2'})
  drawVector(b, 'rs', {'b'}); 
  Pb = A*diag(x); 
  drawVector(Pb, 'g+', {'x_{11}\cdot a_1', 'x_{21}\cdot a_2'});
drawLine([b Pb(:,1)]);
drawLine([b Pb(:,2)]);
hold off; 
title('Ax=b in the column space', 'FontSize', 14);
 
 
 
 
 
 
figure(2); clf; hold on; 
drawPlane(A(1,:), b(1), 'b'); 
drawPlane(A(2,:), b(2), 'g'); 
hold off title('Ax=b in the row space', 'FontSize', 14);

%ASKHSH 2

A = [3 12 1; 12 0 2; 0 2 3];
b = [2.36 5.26 2.77]';
x = A\b
Pb = A*diag(x);
figure(1);clf;hold on;
drawVector(A, {'a_1','a_2','a_3'});
drawVector(b, 'rs', {'b'});
drawLine([b Pb(:,1)]);
hold off;
title('Ax=b in the column space', 'FontSize', 14);
 
figure(2); clf; hold on;
drawPlane(A(1,:), b(1), 'b');
drawPlane(A(2,:), b(2), 'g');
drawPlane(A(3,:), b(3), 'r');
plot3(x(1),x(2),x(3), 'mx', 'LineWidth', 4, 'MarkerSize', 15)
hold off title('Ax=b in the row space', 'FontSize', 14);

 %ASKHSH 3
 
 A = [ 4 1 ; 0 3.5 ];
 b = [2 -3]';
 figure(3);
 clf; hold on;
 drawPlane(A(1,:), b(1), 'b');
 drawPlane(A(2,:), b(2), 'g');
 hold off title('Ax=b in the row space', 'FontSize', 14);
 figure(3);
 hold on;
plot(x(1), x(2), 'rx', 'MarkerSize', 16, 'LineWidth', 2);
hold off;

x = A\b;
figure(2);
clf; hold on; 
drawVector(A, {'a_1', 'a_2'});
drawVector(b, 'rs', {'b'}); 
Pb = A*diag(x);
drawVector(Pb, 'g+', {'x_{11}\cdot a_1', 'x_{21}\cdot a_2'});
drawLine([b Pb(:,1)]); 
drawLine([b Pb(:,2)]);
hold off;
title('Ax=b in the column space', 'FontSize', 14);


%Gia tis askiseis 4-5 leipoun sunartiseis....